package com.stu.serive.impl;

import com.stu.Dto.StuInformationDto;
import com.stu.context.BaseContext;
import com.stu.mapper.StuInformationMapper;
import com.stu.pojo.ImageInfor;
import com.stu.pojo.StuBasicInformation;
import com.stu.pojo.StuFamilyInformation;
import com.stu.pojo.StuOtherInformation;
import com.stu.serive.StuInformationService;
import com.stu.vo.StuInformationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class StuInformationServiceImpl implements StuInformationService {

    @Autowired
    private StuInformationMapper stuInformationMapper;

    /**
     * 新增学生信息
     */
    @Transactional
    public void addWithFamilyWithOther(StuInformationDto stuInformationDto) {
        //将数据分开打包 basic family other
        //得到的对象有多个参数，分成两部分进行传参
        StuBasicInformation stuBasicInformation = new StuBasicInformation();
        BeanUtils.copyProperties(stuInformationDto, stuBasicInformation);
        //System.out.println(stuBasicInformation);

        //向学生信息基础表插入数据
        stuInformationMapper.insert_basic(stuBasicInformation);
        //获取insert语句生成的主键值
        Integer stuId = stuBasicInformation.getId();
        log.info("完成学生基础信息的插入");

        //获取Family列表数据
        List<StuFamilyInformation> stuFamilyInformations = stuInformationDto.getStuFamilyInformation();

        //向家庭表插入数据（数组）
        if(stuFamilyInformations != null && stuFamilyInformations.size() > 0){
            stuFamilyInformations.forEach(FamilyInformation -> {
                FamilyInformation.setStuBasicId(stuId);
            });
            //System.out.println(stuFamilyInformations);
            stuInformationMapper.insert_family(stuFamilyInformations);
        }

        //获取Other数据
        StuOtherInformation stuOtherInformation = stuInformationDto.getStuOtherInformation();
        //向Other表插入数据
        if(stuOtherInformation != null ){
            stuOtherInformation.setStuBasicId(stuId);
            //System.out.println(stuOtherInformation);
            stuInformationMapper.insert_other(stuOtherInformation);
        }
    }

    /**
     * 修改学生信息
     * @param stuInformationDto
     */
    public void updateWithFamilyWithOther(StuInformationDto stuInformationDto) {

        StuBasicInformation stuBasicInformation = new StuBasicInformation();

        //获取学生基本信息
        BeanUtils.copyProperties(stuInformationDto, stuBasicInformation);
        //修改学生基本信息
        stuInformationMapper.update_basic(stuBasicInformation);
        //获取basic主键值
        Integer stuId = stuBasicInformation.getId();
        log.info("完成学生基础信息的插入");

        //获取列表数据
        List<StuFamilyInformation> stuFamilyInformations = stuInformationDto.getStuFamilyInformation();
        //向家庭表插入数据（数组）
        if(stuFamilyInformations != null && stuFamilyInformations.size() > 0){
            //对于数组数据的更新来说，先删除后重新插入会更加简便
            stuInformationMapper.deleteFamilyByBasicId(stuId);

            stuFamilyInformations.forEach(FamilyInformation -> {
                FamilyInformation.setStuBasicId(stuId);
            });
            //System.out.println(stuFamilyInformations);
            stuInformationMapper.insert_family(stuFamilyInformations);
            log.info("获取到新的学生家庭信息，并且完成修改：{}",stuFamilyInformations);
        }

        //获取Other数据
        StuOtherInformation stuOtherInformation = stuInformationDto.getStuOtherInformation();
        //向Other表插入数据
        if(stuOtherInformation != null ){
            stuOtherInformation.setStuBasicId(stuId);
            //System.out.println(stuOtherInformation);
            stuInformationMapper.update_other(stuOtherInformation);
            log.info("获取到新的学生其他信息，并且完成修改：{}",stuOtherInformation);
        }
    }

    /**
     * 查询学生的个人信息
     *
     * @param id
     * @return
     */
    public StuInformationVo selectById(Long id) {
        //创建Vo对象
        StuInformationVo stuInformationVo = new StuInformationVo();

        //获取basic数据
        StuBasicInformation stuBasicInformation = stuInformationMapper.selectBasicById(id);
        log.info("获取stuBasicInformation成功：{}",stuBasicInformation);
        //获取family（数组）数据
        List<StuFamilyInformation> stuFamilyInformations = stuInformationMapper.selectFamilyById(id);
        log.info("获取stuFamilyInformations成功：{}",stuFamilyInformations);
        //获取other数据
        StuOtherInformation stuOtherInformation = stuInformationMapper.selectOtherById(id);
        log.info("获取stuOtherInformation成功：{}",stuOtherInformation);

        //打包数据为Vo
        BeanUtils.copyProperties(stuBasicInformation,stuInformationVo);
        stuInformationVo.setStuFamilyInformation(stuFamilyInformations);
        stuInformationVo.setStuOtherInformation(stuOtherInformation);
        log.info("打包成功：{}",stuInformationVo);

        return stuInformationVo;
    }

    /**
     * 修改或添加学生照片
     * @param image
     */
    public void InsertStuImage(ImageInfor image) {
        //根据当前用户的id修改或添加照片信息
        stuInformationMapper.InsertStuImage(image.getImage(), BaseContext.getCurrentId());
    }
}
